package com.example.scaffold.order.controller;

import com.example.scaffold.common.bean.PageResult;
import com.example.scaffold.common.result.Result;
import com.example.scaffold.order.bean.OrderDTO;
import com.example.scaffold.order.bean.OrderEditParam;
import com.example.scaffold.order.bean.OrderQueryParam;
import com.example.scaffold.order.bean.OrderVO;
import com.example.scaffold.order.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

/**
 * 订单管理控制器
 * 遵循阿里手册：控制器命名规范，接口设计规范
 *
 * @author example
 */
@RestController
@RequestMapping("/order")
@Tag(name = "订单管理", description = "订单相关操作接口")
@RequiredArgsConstructor
public class OrderController {
    private static final Logger log = LoggerFactory.getLogger(OrderController.class);
    
    private final OrderService orderService;

    /**
     * 根据ID查询订单
     *
     * @param id 订单ID
     * @return 订单信息
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询订单", description = "通过订单ID获取订单详细信息")
    public Result<OrderDTO> getOrderById(
            @Parameter(description = "订单ID", required = true)
            @PathVariable Long id) {
        log.info("查询订单信息，订单ID:{}", id);
        return Result.success(orderService.getOrderById(id));
    }

    /**
     * 根据属性查询订单，带有分页
     *
     */
    @PostMapping("/page")
    @Operation(summary = "根据属性查询订单", description = "根据属性查询订单，带有分页")
    public Result<PageResult<OrderVO>> listOrder(@RequestBody OrderQueryParam param) {
        log.info("根据属性查询订单，参数:{}", param);
        return Result.success(orderService.listOrder(param));
    }

    /**
     * 创建订单
     *
     * @param param 订单信息
     * @return 操作结果
     */
    @PostMapping
    @Operation(summary = "创建订单", description = "新增订单信息")
    public Result<Long> createOrder(
            @Parameter(description = "订单信息", required = true)
            @Valid @RequestBody OrderEditParam param) {
        log.info("创建订单，订单信息:{}", param.getUsername());
        Long orderId = orderService.createOrder(param);
        log.info("创建订单成功，订单ID:{}", orderId);
        return Result.success(orderId);
    }

    /**
     * 更新订单
     *
     * @param id      订单ID
     * @param param 订单信息
     * @return 操作结果
     */
    @PutMapping("/{id}")
    @Operation(summary = "更新订单", description = "修改订单信息")
    public Result<Boolean> updateOrder(
            @Parameter(description = "订单ID", required = true)
            @PathVariable Long id,
            @Parameter(description = "订单信息", required = true)
            @Valid @RequestBody OrderEditParam param) {
        log.info("更新订单，订单ID:{}", id);
        return Result.success(orderService.updateOrder(id, param));
    }

    /**
     * 删除订单
     *
     * @param id 订单ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除订单", description = "根据ID删除订单")
    public Result<Boolean> deleteOrder(
            @Parameter(description = "订单ID", required = true)
            @PathVariable Long id) {
        log.info("删除订单，订单ID:{}", id);
        return Result.success(orderService.deleteOrder(id));
    }
}